package com.bing;

import com.bing.Impl.StartStep;
import com.bing.sina.TaoListener;
import com.bing.sina.TaoShua;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.ResourceUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by Administrator on 2014/7/2.
 */
public class Sina {
    private static final Logger logger = LoggerFactory.getLogger(Sina.class);
    public static final String TITLE = "新浪微博采集V3";
    private JTextField _userName;
    private JButton shua;
    private static StartStep startStep;
    private TaoListener taoListener;
    private Properties settings;
    private static JFrame frame;
    private File weiBoFile;

    public Sina() {
        createUIComponents();
        shua.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                logger.info("淘一下");
                taoListener.disableShua("正在用力刷.....");
                String suserName = _userName.getText();
                String spassWord = String.copyValueOf(_password.getPassword());
                String s_weiBoId = _weiBoId.getText();
                new Thread(new TaoShua(startStep, chrome.isSelected() ? "chrome" : "phjs", s_weiBoId, suserName, spassWord, forceUpdate.isSelected(), taoListener)).start();
                settings.setProperty("userName", suserName);
                updateSetting();
            }
        });
        file.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fDialog = new JFileChooser(); //文件选择器
                int result = fDialog.showOpenDialog(frame);
                if (result == JFileChooser.APPROVE_OPTION) {
                    weiBoFile = fDialog.getSelectedFile();
                    _weiBoId.setText(weiBoFile.getAbsolutePath());
                } else {

                }
            }
        });

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        frame = new JFrame(TITLE);
        frame.setContentPane(new Sina().Panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setResizable(false);
        frame.setSize(320, 250);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPasswordField _password;
    private JLabel userName;
    private JLabel password;
    private JPanel Panel1;
    private JLabel status;
    private JTextField _weiBoId;
    private JLabel weiBoId;
    private JRadioButton chrome;
    private JRadioButton phjs;
    private JCheckBox forceUpdate;
    private JTabbedPane weiboTab;
    private JButton file;


    private void createUIComponents() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/bing/conf/application.xml");
        startStep = context.getBean(StartStep.class);
        settings = (Properties) context.getBean("setting");
        taoListener = new TaoListener(shua, status, frame);
        _userName.setText(settings.getProperty("userName", ""));
    }

    private void updateSetting() {
        FileOutputStream bof = null;
        try {
            bof = new FileOutputStream(ResourceUtils.getFile("classpath:com/bing/conf/setting.properties"), false);
            settings.store(bof, "setting");
        } catch (Exception e) {
        } finally {
            if (bof != null) {
                try {
                    bof.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        Panel1 = new JPanel();
        Panel1.setLayout(new GridLayoutManager(3, 1, new Insets(8, 8, 8, 8), -1, -1));
        weiboTab = new JTabbedPane();
        Panel1.add(weiboTab, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(200, 100), null, 0, false));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        weiboTab.addTab("单个人物", panel1);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        panel1.add(panel2);
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel2.add(panel3);
        _userName = new JTextField();
        _userName.setText("贝才礻申亻山");
        panel3.add(_userName, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        userName = new JLabel();
        userName.setText("用户名:");
        panel3.add(userName, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        password = new JLabel();
        password.setText("密码:");
        panel3.add(password, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        _password = new JPasswordField();
        _password.setEditable(true);
        panel3.add(_password, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        weiBoId = new JLabel();
        weiBoId.setText("微博ID:");
        panel3.add(weiBoId, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(50, 27), null, 0, false));
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new BorderLayout(0, 0));
        panel4.setEnabled(true);
        panel3.add(panel4, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, 1, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(239, 27), null, 0, false));
        _weiBoId = new JTextField();
        _weiBoId.setText("3625717890");
        panel4.add(_weiBoId, BorderLayout.CENTER);
        file = new JButton();
        file.setText("文件");
        panel4.add(file, BorderLayout.EAST);
        final JPanel panel5 = new JPanel();
        panel5.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        weiboTab.addTab("特定微博", panel5);
        final JPanel panel6 = new JPanel();
        panel6.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel5.add(panel6, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JPanel panel7 = new JPanel();
        panel7.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel6.add(panel7, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(446, 24), null, 0, false));
        final JTextField textField1 = new JTextField();
        textField1.setText("");
        panel7.add(textField1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("微博内容:");
        panel7.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel8 = new JPanel();
        panel8.setLayout(new GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
        Panel1.add(panel8, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(446, 24), null, 0, false));
        shua = new JButton();
        shua.setText("刷一下");
        panel8.add(shua, new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        chrome = new JRadioButton();
        chrome.setSelected(false);
        chrome.setText("Chrome");
        panel8.add(chrome, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        phjs = new JRadioButton();
        phjs.setEnabled(true);
        phjs.setSelected(true);
        phjs.setText("后台");
        phjs.setToolTipText("没有界面的");
        panel8.add(phjs, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        forceUpdate = new JCheckBox();
        forceUpdate.setText("强制刷新");
        panel8.add(forceUpdate, new GridConstraints(1, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel9 = new JPanel();
        panel9.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        Panel1.add(panel9, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        status = new JLabel();
        status.setHorizontalAlignment(0);
        status.setHorizontalTextPosition(2);
        status.setText("输入信息,开刷.");
        panel9.add(status);
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(phjs);
        buttonGroup.add(chrome);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return Panel1;
    }
}
